//
//  ViewController.swift
//  ImageLoopDemo
//
//  Created by 夏灵烽 on 2025/1/7.
//

import UIKit

class ViewController: UIViewController {

    var loopView: LFLoopView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
                
        loadInfo()
    }

    func setupLoopView(titleArray: Array<String>, imageArray: Array<String>) {
        if loopView == nil {
            loopView = LFLoopView()
            
            loopView?.backgroundColor = UIColor.clear
            loopView?.setViewInfo(titleArray: titleArray, imageArray: imageArray)
            view.addSubview(loopView ?? LFLoopView())
        }
        
        setupLoopViewLayout()
    }
    
    
    func setupLoopViewLayout() {
        if loopView != nil {
            loopView?.translatesAutoresizingMaskIntoConstraints = false
            
            loopView?.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
            loopView?.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
            loopView?.heightAnchor.constraint(equalToConstant: 250).isActive = true
            var width: CGFloat
            if UIScreen.main.bounds.width < UIScreen.main.bounds.height {
                width = UIScreen.main.bounds.width
            }else{
                width = UIScreen.main.bounds.height
            }
            
            loopView?.widthAnchor.constraint(equalToConstant: width).isActive = true
        }
    }
    
    
    func loadInfo() {
        
        let session = URLSession.shared
        guard let url = URL(string: "https://c.m.163.com/nc/ad/headline/0-3.html") else { return }
        
        let task = session.dataTask(with: url) { (data, response, error) in
            if let error = error {
                print("Error: \(error)")
                return
            }
            
            guard let data = data else { return }
            
            do {
                // 使用 JSONSerialization 解析 JSON 数据
                if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
                    // 解析数据
                    if let newsData = json["headline_ad"] as? Array<Dictionary<String, Any>>
                    {
                        let imgsrcArr = newsData.map{ $0["imgsrc"] as? String ?? "" }
                        let titleArr = newsData.map{ $0["title"] as? String ?? "" }
                        
                        print("imgsrcArr: \n \(imgsrcArr)  \n \n  titleArr: \n \(titleArr)")
                        
                        DispatchQueue.main.async {
                            self.setupLoopView(titleArray: titleArr, imageArray: imgsrcArr)
                        }
                        
                    }
                }
            } catch {
                print("JSON 解析失败: \(error.localizedDescription)")
            }
        }
        
        task.resume()
    }

    
//    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//        loopView?.removeFromSuperview()
//        loopView = nil
//    }
    
}

